///////////////////////////
//
// File: bcf_segmente_menu.cpp
//
// Author: Brian Fehrman
//
// Purpose: Contains the class implementation for the Segment menu functions which include such functions as
//          binary thresholding. NOTE that as of now it depends on the Grayscale function contained in the
//          bcf_palette_menu cpp file
//
///////////////////////////

#include "bcf_img_proc.h"

//////////////////////
// Function: Binary Threshold
// Author: Brian Fehrman
//
// Purpose: Takes an image and performs a binary thresholding
///////////////////////
bool BCF_Img_Proc::Menu_Segment_Binary_Threshold(Image &image)
{
    //No point in doing anything if we don't have an image with which to work.
    if ( image.IsNull() ) return false;

    int threshold = 128;

    Dialog dlg("Binary Thresholding");
    dlg.Add( threshold, "Threshold Value");

    if( dlg.Show() )
    {
        int nrows = image.Height();
        int ncols = image.Width();

        if( threshold < 0 )
        {
            threshold = 0;
        }
        else if( threshold > 255 )
        {
            threshold = 255;
        }

        //Convert image to grayscale
        Menu_Palette_Grayscale( image );

        //For each row in the image
        for ( int curr_r = 0; curr_r < nrows; curr_r++ )
        {
            //For each column in the image
            for( int curr_c = 0; curr_c < ncols; curr_c++  )
            {
                //Get intensity value and perform thresholding
                int intensity = ( image[ curr_r ][ curr_c].Intensity() < threshold ) ? 0 : 255;

                //Set each channel to new pixel value
                image[ curr_r ][ curr_c].SetIntensity( intensity );
            }
        }

        //Return true in order to update the image
        return true;
    }
    else
        return false;
}
